package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.common.AppJwtUtil;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {


    @Autowired
    private WmUserMapper wmUserMapper;

    @Override
    public WmUser findByName(String name) {
        if(StringUtils.isNotBlank(name)){
            QueryWrapper<WmUser> queryWrapper =new QueryWrapper<>();
            queryWrapper.lambda().eq(WmUser::getName,name);
            List<WmUser> wmUserList = this.list(queryWrapper);
            if(wmUserList.size()>0){
                return wmUserList.get(0);
            }
        }
        return null;
    }

    @Override
    public boolean saveWmUser(WmUser wmUser) {
        int insert = wmUserMapper.insert(wmUser);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public ResponseResult login(WmUserDto dto) {
        if(StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        LambdaQueryWrapper<WmUser> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmUser::getName,dto.getName());

        List<WmUser> list = this.list(lambdaQueryWrapper);

        if(list!=null && list.size()>0){

            //得到用户
            WmUser wmUser = list.get(0);
            //验证密码
            String newPwd = DigestUtils.md5DigestAsHex((dto.getPassword() + wmUser.getSalt()).getBytes());
            if(wmUser.getPassword().equals(newPwd)){
                //验证通过  用户名和密码对的   jwt token
                Map<String,Object> map =new HashMap<>();
                wmUser.setPassword("");
                wmUser.setSalt("");
                wmUser.setPhone("");
                map.put("user",wmUser);
                map.put("token", AppJwtUtil.getToken(wmUser.getId().longValue()));
                return ResponseResult.okResult(map);
            }else{
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
    }
}
